package com.cogent.business.action.base;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import net.sf.json.JsonConfig;

import org.apache.struts2.ServletActionContext;

import com.cogent.business.action.AbstractAction;
import com.cogent.business.cache.UserCacheManager;
import com.cogent.business.helper.UserDomainHelper;
import com.cogent.business.helper.ValidatorHelper;
import com.cogent.common.io.FileUtil;
import com.cogent.common.util.BaseUtil;
import com.cogent.common.util.Globals;
import com.cogent.common.util.HtmlUtil;
import com.cogent.common.util.MD5;
import com.cogent.common.util.MapUtil;
import com.cogent.dao.BussinessQueryHelper;
import com.cogent.dao.domain.Pagination;
import com.cogent.dao.domain.QueryParamDomain;
import com.cogent.dao.type.QueryType;
import com.cogent.dynamicform.util.DynamicFormBusUtil;
import com.opensymphony.oscache.util.StringUtil;

public class ProductAction extends AbstractAction {
	private static final String RESOURCE_CODE = "SYS_USER";
	private final static String REC_RESOURCE_CODE = "SYS_USER_REC";
	private final static String UPLOAD_DIR = "upload";
	private java.util.List<File> upload;
	private java.util.List<String> uploadFileName;
	private java.util.List<String> uploadContentType;

	public String list() {
		if (!UserDomainHelper.isAllow(ServletActionContext.getRequest(),
				RESOURCE_CODE, Globals.VIEW))
			return "noright";
		HashMap formMap = getFormMapWithUser();
		String sql = "select t.*,pc.name as categoryname from product t,productcategory pc where t.productcategory_id=pc.id ";
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain("product", "name", "t.name", MapUtil
				.getStringValue(formMap, "name"), QueryType.ALLFUZZYMATCH));
		queryList.add(new QueryParamDomain("product", "productsn",
				"t.productsn", MapUtil.getStringValue(formMap, "productsn"),
				QueryType.ALLFUZZYMATCH));
		Pagination pagination = bussinessFacade.queryWithPrefixPaged(sql,
				queryList, "", MapUtil.getIntValue(formMap, "page", 1), MapUtil
						.getIntValue(formMap, "numperpage", Globals
								.getPageSize()));
		setResponseMsg("paginlist", pagination);
		setResponseMsg("formmap", formMap);

		return "list";
	}

	public String typelist() {
		if (!UserDomainHelper.isAllow(ServletActionContext.getRequest(),
				"PRODUCT_TYPE", Globals.VIEW))
			return "noright";
		HashMap formMap = getFormMapWithUser();
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain("name", MapUtil.getStringValue(
				formMap, "name"), QueryType.ALLFUZZYMATCH));
		Pagination pagination = bussinessFacade.queryPaged("producttype",
				queryList, "", MapUtil.getIntValue(formMap, "page", 1), MapUtil
						.getIntValue(formMap, "numperpage", Globals
								.getPageSize()));
		setResponseMsg("paginlist", pagination);
		setResponseMsg("formmap", formMap);
		return "typelist";
	}

	public String typeadd() {
		HashMap formMap = getFormMapWithUser();
		if (!UserDomainHelper.isAllow(ServletActionContext.getRequest(),
				"PRODUCT_TYPE", Globals.SAVE))
			return "noright";
		setResponseMsg("formmap", formMap);
		return "typeadd";
	}

	public String attradd() {
		HashMap formMap = getFormMapWithUser();
		if (!UserDomainHelper.isAllow(ServletActionContext.getRequest(),
				"PRODUCT_TYPE", Globals.SAVE))
			return "noright";
		setResponseMsg("formmap", formMap);
		return "attradd";
	}

	public String categoryadd() {
		HashMap formMap = getFormMapWithUser();
		if (!UserDomainHelper.isAllow(ServletActionContext.getRequest(),
				"PRODUCT_TYPE", Globals.SAVE))
			return "noright";
		List queryList = new ArrayList();
		String sql = "select * from productcategory order by orderlist";
		List categorylist = bussinessFacade.query(sql);
		List childrenList = new ArrayList();

		getChildrenMap(childrenList, categorylist, null, 0);

		setResponseMsg("childrenlist", childrenList);
		setResponseMsg("formmap", formMap);
		return "categoryadd";
	}

	public String categorymod() {
		HashMap formMap = getFormMapWithUser();
		if (!UserDomainHelper.isAllow(ServletActionContext.getRequest(),
				"PRODUCT_TYPE", Globals.SAVE))
			return "noright";
		List queryList = new ArrayList();
		String sql = "select * from productcategory order by orderlist";
		List categorylist = bussinessFacade.query(sql);
		List childrenList = new ArrayList();

		getChildrenMap(childrenList, categorylist, null, 0);
		setResponseMsg("childrenlist", childrenList);
		queryList = new ArrayList();
		queryList.add(new QueryParamDomain("id", MapUtil.getStringValue(
				formMap, "id")));
		HashMap dataMap = bussinessFacade.getByID("productcategory", queryList);
		setResponseMsg("datamap", dataMap);
		setResponseMsg("formmap", formMap);

		return "categorymod";
	}

	public String typemod() {
		HashMap formMap = getFormMapWithUser();
		if (!UserDomainHelper.isAllow(ServletActionContext.getRequest(),
				"PRODUCT_TYPE", Globals.SAVE))
			return "noright";
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain("id", MapUtil.getStringValue(
				formMap, "id")));
		HashMap dataMap = bussinessFacade.getByID("producttype", queryList);
		setResponseMsg("datamap", dataMap);
		setResponseMsg("formmap", formMap);

		return "typemod";
	}

	public String attrmod() {
		HashMap formMap = getFormMapWithUser();
		if (!UserDomainHelper.isAllow(ServletActionContext.getRequest(),
				"PRODUCT_TYPE", Globals.SAVE))
			return "noright";
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain("id", MapUtil.getStringValue(
				formMap, "id")));
		HashMap dataMap = bussinessFacade
				.getByID("productattribute", queryList);
		setResponseMsg("datamap", dataMap);
		setResponseMsg("formmap", formMap);

		return "attrmod";
	}

	public String typesave() {
		if ((!UserDomainHelper.isAllow(ServletActionContext.getRequest(),
				RESOURCE_CODE, Globals.SAVE))
				&& (!UserDomainHelper.isAllow(
						ServletActionContext.getRequest(), RESOURCE_CODE,
						Globals.UPDATE)))
			return "noright";
		HashMap formMap = getFormMapWithUser();
		boolean isAdd = MapUtil.isEmpty(formMap, "id");
		List curQueryList = new ArrayList();
		curQueryList.add(new QueryParamDomain("name", MapUtil.getStringValue(
				formMap, "name"), QueryType.EQUAL));
		HashMap curdataMap = bussinessFacade.getOneRowByTable("producttype",
				curQueryList);
		if (MapUtil.getStringValue(curdataMap, "id") != null
				&& !MapUtil.getStringValue(curdataMap, "id").equals(
						MapUtil.getStringValue(formMap, "id"))) {
			setNoteMsg("ProducttypeForm", "类型名称重复！");
			setSessionInfo("formmap", formMap);
			if (isAdd) {
				setResponseUrl("Product_typeadd.action");
			} else {
				setResponseUrl("Product_typemod.action?id="
						+ MapUtil.getStringValue(formMap, "id"));
			}
			return null;
		}
		bussinessFacade.saveOrUpdate("producttype", formMap);
		String referer = MapUtil.getStringValue(formMap, "referer");
		setResponseUrl(HtmlUtil.getUnEscapeReferer(referer));
		return null;
	}

	public String attrsave() {
		if ((!UserDomainHelper.isAllow(ServletActionContext.getRequest(),
				RESOURCE_CODE, Globals.SAVE))
				&& (!UserDomainHelper.isAllow(
						ServletActionContext.getRequest(), RESOURCE_CODE,
						Globals.UPDATE)))
			return "noright";
		HashMap formMap = getFormMapWithUser();
		boolean isAdd = MapUtil.isEmpty(formMap, "id");
		List curQueryList = new ArrayList();
		curQueryList.add(new QueryParamDomain("name", MapUtil.getStringValue(
				formMap, "name"), QueryType.EQUAL));
		curQueryList.add(new QueryParamDomain("producttype_id", MapUtil
				.getStringValue(formMap, "producttype_id"), QueryType.EQUAL));

		HashMap curdataMap = bussinessFacade.getOneRowByTable(
				"productattribute", curQueryList);
		if (MapUtil.getStringValue(curdataMap, "id") != null
				&& !MapUtil.getStringValue(curdataMap, "id").equals(
						MapUtil.getStringValue(formMap, "id"))) {
			setNoteMsg("ProductattrForm", "商品属性名称在此商品分类中已存在！");
			setSessionInfo("formmap", formMap);
			if (isAdd) {
				setResponseUrl("Product_attradd.action");
			} else {
				setResponseUrl("Product_attrmod.action?id="
						+ MapUtil.getStringValue(formMap, "id"));
			}
			return null;
		}
		formMap.put("attributeoptionstore", MapUtil.getStringValue(formMap, "attributeoptionlist"));
		bussinessFacade.saveOrUpdate("productattribute", formMap);
		UserCacheManager.remove(MapUtil.getStringValue(formMap, "id"));
		String referer = MapUtil.getStringValue(formMap, "referer");
		setResponseUrl(HtmlUtil.getUnEscapeReferer(referer));
		return null;
	}

	public String categorysave() {
		if ((!UserDomainHelper.isAllow(ServletActionContext.getRequest(),
				RESOURCE_CODE, Globals.SAVE))
				&& (!UserDomainHelper.isAllow(
						ServletActionContext.getRequest(), RESOURCE_CODE,
						Globals.UPDATE)))
			return "noright";
		HashMap formMap = getFormMapWithUser();
		boolean isAdd = MapUtil.isEmpty(formMap, "id");
		bussinessFacade.saveOrUpdate("productcategory", formMap);
		String referer = MapUtil.getStringValue(formMap, "referer");
		setResponseUrl(HtmlUtil.getUnEscapeReferer(referer));
		return null;
	}

	public String categorydelete() {
		if ((!UserDomainHelper.isAllow(ServletActionContext.getRequest(),
				RESOURCE_CODE, Globals.SAVE))
				&& (!UserDomainHelper.isAllow(
						ServletActionContext.getRequest(), RESOURCE_CODE,
						Globals.UPDATE)))
			return "noright";
		HashMap formMap = getFormMapWithUser();
		List queryList = new ArrayList();
		String sql = "select * from productcategory";
		queryList.add(new QueryParamDomain("parent_id", MapUtil.getStringValue(
				formMap, "id")));

		List categorylist = bussinessFacade.query("productcategory", queryList);

		if (categorylist.size() > 0) {
			setNoteMsg("ProducttypeForm", "存在子分类不能删除！");
			setSessionInfo("formmap", formMap);
		} else {
			queryList = new ArrayList();
			queryList.add(new QueryParamDomain("id", MapUtil.getStringValue(
					formMap, "id")));
			bussinessFacade.delete("productcategory", queryList);
		}
		String referer = MapUtil.getStringValue(formMap, "referer");
		setResponseUrl("Product_categorylist.action");
		return null;
	}

	public String attrlist() {
		if (!UserDomainHelper.isAllow(ServletActionContext.getRequest(),
				RESOURCE_CODE, Globals.VIEW))
			return "noright";
		HashMap formMap = getFormMapWithUser();
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain("productattribute", "name",
				"pa.name", MapUtil.getStringValue(formMap, "name"),
				QueryType.ALLFUZZYMATCH));
		queryList.add(new QueryParamDomain("productattribute",
				"producttype_id", "pa.producttype_id", MapUtil.getStringValue(
						formMap, "producttype_id"), QueryType.EQUAL));
		String sql = "select pa.*,pt.name as typename from productattribute pa,producttype pt where pa.producttype_id=pt.id";
		Pagination pagination = bussinessFacade.queryWithPrefixPaged(sql,
				queryList, " order by pa.producttype_id,orderlist ", MapUtil
						.getIntValue(formMap, "page", 1), MapUtil.getIntValue(
						formMap, "numperpage", Globals.getPageSize()));
		// bussinessFacade.queryPaged("productattribute", queryList, "",
		// MapUtil.getIntValue(formMap, "page", 1)
		// , MapUtil.getIntValue(formMap, "numperpage", Globals.getPageSize()));
		setResponseMsg("paginlist", pagination);
		HashMap pmap = BussinessQueryHelper.getValueById("producttype", MapUtil
				.getStringValue(formMap, "producttype_id"));
		formMap.put("type", MapUtil.getStringValue(pmap, "name"));
		setResponseMsg("formmap", formMap);
		return "attrlist";
	}

	public String categorylist() {
		if (!UserDomainHelper.isAllow(ServletActionContext.getRequest(),
				RESOURCE_CODE, Globals.VIEW))
			return "noright";
		HashMap formMap = getFormMapWithUser();
		List queryList = new ArrayList();
		String sql = "select * from productcategory order by orderlist";
		List categorylist = bussinessFacade.query(sql);
		List childrenList = new ArrayList();

		getChildrenMap(childrenList, categorylist, null, 0);

		setResponseMsg("childrenlist", childrenList);

		setResponseMsg("formmap", formMap);
		return "categorylist";
	}

	private boolean getChildrenMap(List childrenList, List categorylist,
			String parentid, int level) {
		boolean falg = false;
		Iterator it = categorylist.iterator();
		while (it.hasNext()) {
			HashMap valmap = (HashMap) it.next();
			if (parentid == null) {
				if (MapUtil.isEmpty(valmap, "parent_id")) {
					valmap.put("level", "" + level);
					falg = true;
					childrenList.add(valmap);
					valmap.put("ischrid", getChildrenMap(childrenList,
							categorylist, MapUtil.getStringValue(valmap, "id"),
							level + 1));
					// categorylist.remove(valmap);
				}
			} else {
				if (parentid
						.equals(MapUtil.getStringValue(valmap, "parent_id"))) {
					valmap.put("level", "" + level);
					childrenList.add(valmap);
					falg = true;
					valmap.put("ischrid", getChildrenMap(childrenList,
							categorylist, MapUtil.getStringValue(valmap, "id"),
							level + 1));
					// categorylist.remove(valmap);
				}
			}
		}
		return falg;
	}

	public String add() {
		HashMap formMap = getFormMapWithUser();
		if (!UserDomainHelper.isAllow(ServletActionContext.getRequest(),
				RESOURCE_CODE, Globals.SAVE))
			return "noright";
		List queryList = new ArrayList();
		String sql = "select * from productcategory order by orderlist";
		List categorylist = bussinessFacade.query(sql);
		List childrenList = new ArrayList();

		getChildrenMap(childrenList, categorylist, null, 0);
		setResponseMsg("childrenlist", childrenList);

		queryList = new ArrayList();
		sql = "select * from brand order by orderlist";
		List brandlist = bussinessFacade.query(sql);
		setResponseMsg("brandlist", brandlist);

		queryList = new ArrayList();
		sql = "select * from producttype";
		List typelist = bussinessFacade.query(sql);
		setResponseMsg("typelist", typelist);

		setResponseMsg("formmap", formMap);
		return "add";
	}

	public String ajaxProductAttribute() {
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(new String[] { "createdate", "modifydate",
				"producttype" });
		HashMap formMap = getFormMapWithUser();
		List attrlist = new ArrayList();
		if (!MapUtil.isEmpty(formMap, "producttype_id")) {
			List queryList = new ArrayList();
			String sql = "select * from productattribute where isenabled=1 ";
			queryList.add(new QueryParamDomain("productattribute",
					"producttype_id", "producttype_id", MapUtil.getStringValue(
							formMap, "producttype_id"), QueryType.EQUAL));
			attrlist = bussinessFacade.query(sql, queryList,
					"order by orderlist");
		}
		responseMsg(MapUtil.listMapToJsonStr(attrlist));
		// JSONArray jsonArray =
		// JSONArray.fromObject(productType.getEnabledProductAttributeList(),
		// jsonConfig);
		return null;
	}

	public String mod() {
		HashMap formMap = getFormMapWithUser();
		if(!UserDomainHelper.isAllow(ServletActionContext.getRequest(), RESOURCE_CODE, Globals.SAVE))
			return "noright";
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain("id", MapUtil.getStringValue(formMap, "id")));
		HashMap dataMap = bussinessFacade.getByID("product", queryList);
		
		 queryList = new ArrayList();
	      String sql="select * from productattribute where isenabled=1 ";
	      queryList.add(new QueryParamDomain("productattribute","producttype_id", "producttype_id",MapUtil.getStringValue(dataMap, "producttype_id"), QueryType.EQUAL));
		  List attrlist=bussinessFacade.query(sql, queryList, "order by orderlist"); 
          
		  
		  queryList = new ArrayList();
	       sql="select * from product_productattributemapstore";
	      queryList.add(new QueryParamDomain("product_productattributemapstore","product_id", "product_id",MapUtil.getStringValue(dataMap, "id"), QueryType.EQUAL));
		  List mapkeylist=bussinessFacade.query(sql, queryList, ""); 
        
		  
		  Iterator it=mapkeylist.iterator(); 
        while(it.hasNext()){
      	  HashMap attrmap=(HashMap)it.next();
      	dataMap.put(MapUtil.getStringValue(attrmap, "mapkey_id"), MapUtil.getStringValue(attrmap, "element"));
       	  }
        queryList = new ArrayList();
		  sql="select * from productcategory order by orderlist";
		 List categorylist=bussinessFacade.query(sql); 
		 List childrenList=new ArrayList();
		 
		 getChildrenMap(childrenList,categorylist,null,0); 
		 setResponseMsg("childrenlist", childrenList); 
		 
		 
		  queryList = new ArrayList();
	      sql="select * from brand order by orderlist";
		 List brandlist=bussinessFacade.query(sql); 
		 setResponseMsg("brandlist", brandlist); 
		 
		 queryList = new ArrayList();
	      sql="select * from producttype";
		 List typelist=bussinessFacade.query(sql); 
		 setResponseMsg("typelist", typelist); 
		 
		 
		 
			  it = attrlist.iterator();
			 String productAttributeTrHtml="";
			while(it.hasNext()) {
				HashMap valMap = (HashMap)it.next();  
			if(MapUtil.isEqual(valMap,"attributetype","0")) {
			productAttributeTrHtml += "<tr class='productAttributeContentTr'><td class='shover' >" + MapUtil.getStringValue(valMap,"name") + ":</td><td><input type='text' name='" + MapUtil.getStringValue(valMap,"id") + "' value='"+MapUtil.getStringValue(dataMap,MapUtil.getStringValue(valMap,"id"))+"'  " + (MapUtil.isEqual(valMap,"required","true") ? "class='formText {required: true}'" : "class='formText'") + " />" + (MapUtil.isEqual(valMap,"required","true")? "<span class='txt_red'>*</span>" : "") +"</td></tr>";
		  
			} else if(MapUtil.isEqual(valMap,"attributetype","1")) {
			productAttributeTrHtml += "<tr class='productAttributeContentTr'><td class='shover'>" + MapUtil.getStringValue(valMap,"name") + ":</td><td><input type='text' name='" + MapUtil.getStringValue(valMap,"id") + "'  value='"+MapUtil.getStringValue(dataMap,MapUtil.getStringValue(valMap,"id"))+"' " + (MapUtil.isEqual(valMap,"required","true")? "class='formText {required: true, number: true}'" : " class='formText {number: true}'") + " />" + (MapUtil.isEqual(valMap,"required","true")? "<span class='txt_red'>*</span>" : "") + "</td></tr>";
		} else if(MapUtil.isEqual(valMap,"attributetype","2")){
			productAttributeTrHtml += "<tr class='productAttributeContentTr'><td class='shover'>" +MapUtil.getStringValue(valMap,"name") + ":</td><td><input type='text' name='" + MapUtil.getStringValue(valMap,"id")+ "' value='"+MapUtil.getStringValue(dataMap,MapUtil.getStringValue(valMap,"id"))+"' " + (MapUtil.isEqual(valMap,"required","true")? "class='formText {required: true, lettersonly: true}'" : " class='formText {lettersonly: true}'") + " />" + ((MapUtil.getStringValue(valMap,"required") == "true")? "<span class='txt_red'>*</span>" : "") + "</td></tr>";
		} else if(MapUtil.isEqual(valMap,"attributetype","3")) {
			String productAttributeOption = "<option value=''>请选择...</option>"; 
			String[] attris=MapUtil.getStringValue(valMap,"attributeoptionstore").split(","); 
			for(String key :attris) { 
				 productAttributeOption += "<option value='" + key + "'"+(MapUtil.isEqual(dataMap,MapUtil.getStringValue(valMap,"id"),key)?"selected":"") +" >" + key + "</option>";
			}
 		productAttributeTrHtml += "<tr class='productAttributeContentTr'><td class='shover'>" + MapUtil.getStringValue(valMap,"name")+ ":</td><td><select name='" + MapUtil.getStringValue(valMap,"id") + "'" + (MapUtil.isEqual(valMap,"required","true")? " class='{required: true}'" : "") + ">" + productAttributeOption + "</select>" + (MapUtil.isEqual(valMap,"required","true")? "<span class='txt_red'>*</span>" : "") + "</td></tr>";
		} else if(MapUtil.isEqual(valMap,"attributetype","4")) {
			String  productAttributeOption = "";
		
			String[] attris=MapUtil.getStringValue(valMap,"attributeoptionstore").split(","); 
			for(String key :attris) { 
 				productAttributeOption += "<label><input type='checkbox' name='" +  MapUtil.getStringValue(valMap,"id") + "' "+(MapUtil.isEqual(dataMap,MapUtil.getStringValue(valMap,"id"),key)?"checked":"")+"  value='" + key + '"' + (MapUtil.isEqual(valMap,"required","true") ? " class='{required: true, messagePosition: \"#" + MapUtil.getStringValue(valMap,"id") + "MessagePosition\"}'" : "") +" />" +key + "</label>&nbsp;&nbsp;";
 			}
			productAttributeTrHtml += "<tr class='productAttributeContentTr'><td class='shover'>" + MapUtil.getStringValue(valMap,"name") + ":</td><td>" + productAttributeOption + (MapUtil.isEqual(valMap,"required","true")? "<span id='" + MapUtil.getStringValue(valMap,"id") + "MessagePosition'></span><label class='requireField'>*</label>" : "") + "</td></tr>";
		} else if(MapUtil.getStringValue(valMap,"attributetype")  == "5") {
			productAttributeTrHtml += "<tr class='productAttributeContentTr'><td class='shover'>" + MapUtil.getStringValue(valMap,"name") + ":</td><td><input type='text' name='" + MapUtil.getStringValue(valMap,"id") + "'" + (MapUtil.isEqual(valMap,"required","true")? "class='formText datePicker {required: true, dateISO: true}'" : "class='formText datePicker {dateISO: true}'") + " />" + ((MapUtil.getStringValue(valMap,"required") == "true")? "<span class='txt_red'>*</span>" : "") + "</td></tr>";
		} 
						   } 
		 
			setResponseMsg("productAttributeTrHtml", productAttributeTrHtml);
		 
    	setResponseMsg("attrlist", attrlist);
		setResponseMsg("datamap", dataMap);
		setResponseMsg("formmap", formMap);
		
		return "mod";
	}

	public String view() {
		HashMap formMap = getFormMapWithUser();
		if ((!UserDomainHelper.isAllow(ServletActionContext.getRequest(),
				RESOURCE_CODE, Globals.VIEW))
				&& (!UserDomainHelper.isAllow(
						ServletActionContext.getRequest(), REC_RESOURCE_CODE,
						Globals.VIEW)))
			return "noright";
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain("id", MapUtil.getStringValue(
				formMap, "id")));
		HashMap dataMap = bussinessFacade.getByID("admin", queryList);
		setResponseMsg("datamap", dataMap);
		setResponseMsg("formmap", formMap);

		return "view";
	}

	public String save() {
		if ((!UserDomainHelper.isAllow(ServletActionContext.getRequest(),
				RESOURCE_CODE, Globals.SAVE))
				&& (!UserDomainHelper.isAllow(
						ServletActionContext.getRequest(), RESOURCE_CODE,
						Globals.UPDATE)))
			return "noright";
		try {
			bussinessFacade.createConn();
			bussinessFacade.beginTran();
			HashMap formMap = getFormMapWithUser();
			boolean isAdd = MapUtil.isEmpty(formMap, "id");
			if (MapUtil.isEmpty(formMap, "productsn")) {
				formMap.put("productsn", BaseUtil.genUUID().toUpperCase()
						.substring(15));
			}

			if (isAdd) {
				formMap.put("freezestore", "0");
			}

			// 图片
			String photosrc = MapUtil.getStringValue(formMap, "photosrc");
			if ((upload != null) && (upload.size() > 0)) {

				String realfileName = "";
				for (int i = 0; i < upload.size(); i++) {
					File photo = (File) upload.get(i);
					String postfile = FileUtil.getFilePostfix(uploadFileName
							.get(i));
					String curuuid = BaseUtil.genUUID();
					String fileName = UPLOAD_DIR + File.separator + curuuid
							+ "." + postfile;
					FileUtil.copyFile(photo, Globals.getProperty("uploaddir")
							+ fileName, true);
					photosrc += Globals.getProperty("uploadurl") + UPLOAD_DIR
							+ "/" + curuuid + "." + postfile + ";";
					realfileName += uploadFileName.get(i) + ";";
					FileUtil.deleteFile(photo);
				}
				formMap.put("imgsrc", photosrc);
			}

			List curQueryList = new ArrayList();
			curQueryList.add(new QueryParamDomain("productsn", MapUtil
					.getStringValue(formMap, "productsn"), QueryType.EQUAL));
			HashMap curdataMap = bussinessFacade.getOneRowByTable("product",
					curQueryList);
			if (MapUtil.getStringValue(curdataMap, "id") != null
					&& !MapUtil.getStringValue(curdataMap, "id").equals(
							MapUtil.getStringValue(formMap, "id"))) {
				setNoteMsg("ProductForm", "货号存在重复！");
				setSessionInfo("formmap", formMap);
				if (isAdd) {
					setResponseUrl("Product_add.action");
				} else {
					setResponseUrl("Product_mod.action?id="
							+ MapUtil.getStringValue(formMap, "id"));
				}
				return null;
			}

			String errmsg = ValidatorHelper.validator("ProductForm", formMap);
			if (!StringUtil.isEmpty(errmsg)) {
				setNoteMsg("ProductForm", errmsg);
				setSessionInfo("formmap", formMap);
				setResponseUrl(formMap, "Product_mod.action");
				return null;
			}
			String productid = bussinessFacade.saveOrUpdate("product", formMap);

			List queryList = new ArrayList();
			String sql = "select * from productattribute where isenabled=1 ";
			queryList.add(new QueryParamDomain("productattribute",
					"producttype_id", "producttype_id", MapUtil.getStringValue(
							formMap, "producttype_id"), QueryType.EQUAL));
			List attrlist = bussinessFacade.query(sql, queryList,
					"order by orderlist");
			Iterator it = attrlist.iterator();
			while (it.hasNext()) {
				HashMap attrmap = (HashMap) it.next();
				HashMap valMap = new HashMap();
				valMap.put("product_id", productid);
				valMap.put("mapkey_id", MapUtil.getStringValue(attrmap, "id"));
				valMap.put("element", MapUtil.getStringValue(formMap, MapUtil
						.getStringValue(attrmap, "id")));
				bussinessFacade
						.save("product_productattributemapstore", valMap);
			}

			UserCacheManager.remove(MapUtil.getStringValue(formMap, "id"));
			setResponseUrl(formMap, "Product_list.action");
			bussinessFacade.commitTran();
		} catch (Exception e) {
			bussinessFacade.rollbackTran();
		} finally {
			bussinessFacade.closeConn();
		}
		return null;
	}

	public String reset() {
		if (!UserDomainHelper.isAllow(ServletActionContext.getRequest(),
				RESOURCE_CODE, Globals.UPDATE))
			return "noright";
		HashMap formMap = getFormMapWithUser();
		HashMap dataMap = new HashMap();
		dataMap.put("id", MapUtil.getStringValue(formMap, "id"));
		dataMap.put("modifier", MapUtil.getStringValue(formMap, "modifier"));
		dataMap
				.put("modifyDate", MapUtil
						.getStringValue(formMap, "modifydate"));
		MD5 md5 = new MD5();
		dataMap.put("password", md5.getMD5ofStr("666666"));
		bussinessFacade.update("admin", dataMap);
		setResponseUrl(formMap, "SysUser_list.action");

		return null;
	}

	public java.util.List<File> getUpload() {
		return upload;
	}

	public void setUpload(java.util.List<File> upload) {
		this.upload = upload;
	}

	public java.util.List<String> getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(java.util.List<String> uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public java.util.List<String> getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(java.util.List<String> uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

}
